package com.bianmaba.controllers.datacollection;

import com.bianmaba.beans.datacollection.CollectionSchedule;
import com.bianmaba.commons.annotation.WrapResult;
import com.bianmaba.commons.bean.result.OperationResult;
import com.bianmaba.controllers.base.support.CrudControllerSwaggerSupport;
import com.bianmaba.services.datacollection.ICollectionScheduleService;
import com.bianmaba.services.datacollection.quartiz.QuartzTaskManager;
import com.bianmaba.spring.mvc.annotation.RequestResponseBodyConfig;
import io.swagger.annotations.Api;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api(description = "采集计划API")
@RestController
@RequestMapping(value = "datacollection/collectionschedule")
public class CollectionScheduleController extends CrudControllerSwaggerSupport<CollectionSchedule, String> {
    @Autowired
    private ICollectionScheduleService service;

    @Autowired
    private QuartzTaskManager quartzTaskManager;


    protected ICollectionScheduleService getService() {
        return service;
    }

    @Override
    protected OperationResult<CollectionSchedule> save(CollectionSchedule entity) {
        try {
            CollectionSchedule schedule = service.saveAndStartSchedule(entity);
            return OperationResult.of(true).setData(schedule).setResult("计划保存成功！");
        } catch (ClassNotFoundException e) {
            return OperationResult.of(true).setResult("计划保存失败：执行计划时找不到对应的任务处理类！");
        } catch (Exception e) {
            return OperationResult.of(true).setResult("计划保存出现异常！");
        }
    }
//
//    @RequestMapping("schedule")
//    public Trigger schedule(String name, String group, String cronExpression) throws ClassNotFoundException {
//        Class clazz = Class.forName("com.bianmaba.jobs.DefaultJob");
//        JobDataMap data = new JobDataMap();
//        data.put("description", "scheduleJob is running...");
//
//        return quartzTaskManager.scheduleJob(clazz, name, group, cronExpression, data);
//    }
//
//
//    @RequestMapping("unschedule")
//    public void unscheduleJob(String name, String group) throws ClassNotFoundException, SchedulerException {
//        quartzTaskManager.unscheduleJob(name, group);
//    }
//
//    @RequestMapping("reschedule")
//    public Trigger reschedule(String name, String group, String cronExpression) throws ClassNotFoundException {
//        return quartzTaskManager.rescheduleJob(name, group, "0/5 * * * * ?");
//    }
//
//    @Override
//    @RequestResponseBodyConfig(excludeProperties = {"data.*\\.collectionInterface"})
//    public QueryResult<List<CollectionSchedule>> query(@EntityParam LinkedHashMap<String, Object> params, @EntityParam PageableRequest pageable) {
//        return super.query(params, pageable);
//    }

    /**
     * 启动单次执行的任务
     *
     * @param taskId
     * @param executor
     * @return
     */
    @RequestResponseBodyConfig
    @RequestMapping("execute_once")
    @WrapResult
    public OperationResult executeOnce(String taskId, String executor, String startDate, String endDate) {
        try {
            Map<String, String> params = new HashMap<>();
            params.put("startDate", startDate);
            params.put("endDate", endDate);
            Trigger trigger = service.executeOnce(taskId, executor, params);
            return OperationResult.of(true).setData(trigger).setResult("采集任务提交成功！");
        } catch (ClassNotFoundException e) {
            return OperationResult.of(false).setResult("任务执行失败，找不到对应的任务处理类！");
        }
    }

    @RequestResponseBodyConfig
    @RequestMapping("stop_job")
    @WrapResult
    public OperationResult stopJob(String taskInstanceId) {
        OperationResult result = service.stopJob(taskInstanceId);
        return result;
    }
}
